﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class Main_ServiceInvoice_SalesInvoice : System.Web.UI.Page
{
    Datautility DU = new Datautility();

    #region Declare list ControlID

    int counter = 2;

    List<string> listcounter;

    List<string> listtdservicectrl;
    List<string> listddlservice;

    List<string> listhidTaxableNonTaxable;

    List<string> listtddescriptionctrl;
    List<string> listtxtdescriptionctrl;

    List<string> listtdyearrefctrl;
    List<string> listtxtyearrefctrl;

    List<string> listtduomctrl;
    List<string> listtxtuomctrl;

    List<string> listtdunit2ctrl;
    List<string> listtxtunit2ctrl;
    List<string> listtdunit3ctrl;
    List<string> listtxtunit3ctrl;
    List<string> listtdunit4ctrl;
    List<string> listtxtunit4ctrl;

    List<string> listtdunitrate1ctrl;
    List<string> listtxtunitrate1ctrl;
    List<string> listtdunitrate2ctrl;
    List<string> listtxtunitrate2ctrl;

    List<string> listtdgrossamtctrl;
    List<string> listtxtgrossamtctrl;

    List<string> listtddiscount1ctrl;
    List<string> listtxtdiscount1ctrl;
    List<string> listtddiscount2ctrl;
    List<string> listtxtdiscount2ctrl;

    List<string> listtdservicecharge1crtl;
    List<string> listtxtservicecharge1crtl;
    List<string> listhidServiceCharge;
    List<string> listtdservicecharge2crtl;
    List<string> listtxtservicecharge2crtl;
    List<string> listtdservicecharge3crtl;
    List<string> listtxtservicecharge3crtl;

    List<string> listtdserviceTax1crtl;
    List<string> listtxtserviceTax1crtl;
    List<string> listtdserviceTax2crtl;
    List<string> listtxtserviceTax2crtl;
    List<string> listtdserviceTax3crtl;
    List<string> listtxtserviceTax3crtl;
    List<string> listtdserviceTax4crtl;
    List<string> listtxtserviceTax4crtl;
    List<string> listtdserviceTax5crtl;
    List<string> listtxtserviceTax5crtl;

    List<string> listtdTaxincGrandTotalctrl;
    List<string> listtxtTaxincGrandTotalctrl;

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        OverrideAddMore();
    }

    #endregion End Declare ControlID

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            #region Initilize ControlID

            listcounter = new List<string>();
            ViewState["listcounter"] = listcounter;
          
            listtdservicectrl = new List<string>();
            ViewState["listtdservicectrl"] = listtdservicectrl;
            listddlservice = new List<string>();
            ViewState["listddlservice"] = listddlservice;

            listhidTaxableNonTaxable = new List<string>();
            ViewState["listhidTaxableNonTaxable"] = listhidTaxableNonTaxable;


            listtddescriptionctrl = new List<string>();
            ViewState["listtddescriptionctrl"] = listtddescriptionctrl;
            listtxtdescriptionctrl =new  List<string>();
            ViewState["listtxtdescriptionctrl"] = listtxtdescriptionctrl;

            listtdyearrefctrl = new List<string>();
            ViewState["listtdyearrefctrl"] = listtdyearrefctrl;
            listtxtyearrefctrl = new List<string>();
            ViewState["listtxtyearrefctrl"] = listtxtyearrefctrl;

            listtduomctrl = new List<string>();
            ViewState["listtduomctrl"] = listtduomctrl;
            listtxtuomctrl = new List<string>();
            ViewState["listtxtuomctrl"] = listtxtuomctrl;

            listtdunit2ctrl = new List<string>();
            ViewState["listtdunit2ctrl"] = listtdunit2ctrl;
            listtxtunit2ctrl = new List<string>();
            ViewState["listtxtunit2ctrl"] = listtxtunit2ctrl;
            listtdunit3ctrl = new List<string>();
            ViewState["listtdunit3ctrl"] = listtdunit3ctrl;
            listtxtunit3ctrl = new List<string>();
            ViewState["listtxtunit3ctrl"] = listtxtunit3ctrl;
            listtdunit4ctrl = new List<string>();
            ViewState["listtdunit4ctrl"] = listtdunit4ctrl;
            listtxtunit4ctrl = new List<string>();
            ViewState["listtxtunit4ctrl"] = listtxtunit4ctrl;

            listtdunitrate1ctrl = new List<string>();
            ViewState["listtdunitrate1ctrl"] = listtdunitrate1ctrl;
            listtxtunitrate1ctrl = new List<string>();
            ViewState["listtxtunitrate1ctrl"] = listtxtunitrate1ctrl;
            listtdunitrate2ctrl = new List<string>();
            ViewState["listtdunitrate2ctrl"] = listtdunitrate2ctrl;
            listtxtunitrate2ctrl = new List<string>();
            ViewState["listtxtunitrate2ctrl"] = listtxtunitrate2ctrl;

            listtdgrossamtctrl = new List<string>();
            ViewState["listtdgrossamtctrl"] = listtdgrossamtctrl;
            listtxtgrossamtctrl = new List<string>();
            ViewState["listtxtgrossamtctrl"] = listtxtgrossamtctrl;

            listtddiscount1ctrl = new List<string>();
            ViewState["listtddiscount1ctrl"] = listtddiscount1ctrl;
            listtxtdiscount1ctrl = new List<string>();
            ViewState["listtxtdiscount1ctrl"] = listtxtdiscount1ctrl;
            listtddiscount2ctrl = new List<string>();
            ViewState["listtddiscount2ctrl"] = listtddiscount2ctrl;
            listtxtdiscount2ctrl = new List<string>();
            ViewState["listtxtdiscount2ctrl"] = listtxtdiscount2ctrl;

            listtdservicecharge1crtl = new List<string>();
            ViewState["listtdservicecharge1crtl"] = listtdservicecharge1crtl;
            listtxtservicecharge1crtl = new List<string>();
            ViewState["listtxtservicecharge1crtl"] = listtxtservicecharge1crtl;
            listhidServiceCharge = new List<string>();
            ViewState["listhidServiceCharge"] = listhidServiceCharge;
            listtdservicecharge2crtl = new List<string>();
            ViewState["listtdservicecharge2crtl"] = listtdservicecharge2crtl;
            listtxtservicecharge2crtl = new List<string>();
            ViewState["listtxtservicecharge2crtl"] = listtxtservicecharge2crtl;
            listtdservicecharge3crtl = new List<string>();
            ViewState["listtdservicecharge3crtl"] = listtdservicecharge3crtl;
            listtxtservicecharge3crtl = new List<string>();
            ViewState["listtxtservicecharge3crtl"] = listtxtservicecharge3crtl;

            listtdserviceTax1crtl = new List<string>();
            ViewState["listtdserviceTax1crtl"] = listtdserviceTax1crtl;
            listtxtserviceTax1crtl = new List<string>();
            ViewState["listtxtserviceTax1crtl"] = listtxtserviceTax1crtl;
            listtdserviceTax2crtl = new List<string>();
            ViewState["listtdserviceTax2crtl"] = listtdserviceTax2crtl;
            listtxtserviceTax2crtl = new List<string>();
            ViewState["listtxtserviceTax2crtl"] = listtxtserviceTax2crtl;
            listtdserviceTax3crtl = new List<string>();
            ViewState["listtdserviceTax3crtl"] = listtdserviceTax3crtl;
            listtxtserviceTax3crtl = new List<string>();
            ViewState["listtxtserviceTax3crtl"] = listtxtserviceTax3crtl;
            listtdserviceTax4crtl = new List<string>();
            ViewState["listtdserviceTax4crtl"] = listtdserviceTax4crtl;
            listtxtserviceTax4crtl = new List<string>();
            ViewState["listtxtserviceTax4crtl"] = listtxtserviceTax4crtl;
            listtdserviceTax5crtl = new List<string>();
            ViewState["listtdserviceTax5crtl"] = listtdserviceTax5crtl;
            listtxtserviceTax5crtl = new List<string>();
            ViewState["listtxtserviceTax5crtl"] = listtxtserviceTax5crtl;

            listtdTaxincGrandTotalctrl = new List<string>();
            ViewState["listtdTaxincGrandTotalctrl"] = listtdTaxincGrandTotalctrl;
            listtxtTaxincGrandTotalctrl = new List<string>();
            ViewState["listtxtTaxincGrandTotalctrl"] = listtxtTaxincGrandTotalctrl;

            #endregion End Initilize ControlID

            BindFormat();
            BindCustomer();
            ddlFormat_SelectedIndex(this, EventArgs.Empty);
            //BindService(ddlservice1);
            //BindService(ddlservice2);
            
            
        }
        BindTax();
    }
    protected void BindFormat()
    {
        SqlParameter[] param = { 
                                 new SqlParameter("@TRANSACTIONTYPE","1")
                               };
        DataTable dtformate = DU.getdatausingprocedure("USP_IVC_GetAllTransactionFormatNameID", param);
        if (dtformate.Rows.Count > 0)
        {
            ddlFormat.DataSource = dtformate;
            ddlFormat.DataTextField = "FORMATNAME";
            ddlFormat.DataValueField = "FORMATID";
            ddlFormat.DataBind();
            ddlFormat.SelectedIndex = 0;
            //ddlFormat.Items.Insert(0, new ListItem("-- Select Format --", "0"));
        }
    }
    protected void ddlFormat_SelectedIndex(object sender, EventArgs e)
    {
        ClearTable();
        GetDynamicColumn();
        BindService(ddlservice1);
        BindService(ddlservice2);
        ResetTableControl(tblmain);
        ResetTableControl(tblcalculation);
        txtdate.Text = string.Empty;

        txtPrefix.Text = string.Empty;
        txtbillno.Text = string.Empty;
        txtSuffix.Text = string.Empty;
        if (ddlFormat.SelectedValue != "0")
        {
            string strFinyear = "";
            if (Session["FINYEAR"] != null)
            {
                strFinyear = Session["FINYEAR"].ToString();
            }
            if (strFinyear != string.Empty)
            {
                SqlParameter[] sparam =
                                    {
                                        new SqlParameter("@TFOID",ddlFormat.SelectedValue),
                                        new SqlParameter ("@FINYEAR",strFinyear)
                                    };
                DataTable DTVoucher = DU.getdatausingprocedure("USP_IVC_GetMaxVoucherNo", sparam);
                if (DTVoucher.Rows.Count > 0)
                {
                    txtPrefix.Text = DTVoucher.Rows[0]["prefix"].ToString();
                    txtbillno.Text = DTVoucher.Rows[0]["vouno"].ToString();
                    txtSuffix.Text = DTVoucher.Rows[0]["suffix"].ToString();
                }
            }
            else
            {
                Response.Redirect("~/SessionExpired.aspx");
            }
            //ClearTable();
            BindCustomerPrefence();
        }
        
    }

    protected void BindCustomer()
    {
        DataTable dtCustomer = DU.getdatausingprocedure("USP_IVC_GetCustomerLedger");
        if (dtCustomer.Rows.Count > 0)
        {
            ddlCutstomer.DataSource = dtCustomer;
            ddlCutstomer.DataTextField = "LNAME";
            ddlCutstomer.DataValueField = "LID";
            ddlCutstomer.DataBind();

            ListItem li = new ListItem("-- Select Customer --", "0");
            ddlCutstomer.Items.Insert(0, li);
            li.Selected = true;
        }
    }

    protected void ddlCutstomer_SelectedIndex(object sender, EventArgs e)
    {
        txtaddress.Text = string.Empty;
        if (ddlCutstomer.SelectedValue != "0")
        {
            SqlParameter[] parmaled = { 
                                    new SqlParameter("@LID",ddlCutstomer.SelectedValue)
                                  };
            DataTable dtledadd = DU.getdatausingprocedure("USP_ACC_GetLedgerAddressDetails", parmaled);
            if (dtledadd.Rows.Count > 0)
            {
                txtaddress.Text = dtledadd.Rows[0]["LNAME"].ToString() + " " + dtledadd.Rows[0]["ADDRESS1"].ToString() + " " + dtledadd.Rows[0]["ADDRESS2"].ToString() + " " + dtledadd.Rows[0]["ADDRESS3"].ToString() + " " + dtledadd.Rows[0]["PLACE"].ToString() + " " + dtledadd.Rows[0]["STATENAME"].ToString();
            }
        }

        ClearTable();
        GetDynamicColumn();
        BindCustomerPrefence();
    }

    protected void BindService(DropDownList ddl)
    {
        if (ddlFormat.SelectedValue != "0")
        {
            //string sql = "SELECT SERVICENAME,SERVICEID FROM IVC_SERVICEMASTER INNER JOIN IVC_TRANSACTIONFORMAT_TAX_DISCOUNT_DETAILS ON IVC_SERVICEMASTER.TAXTYPEID=IVC_TRANSACTIONFORMAT_TAX_DISCOUNT_DETAILS.TAXTYPEID WHERE IVC_TRANSACTIONFORMAT_TAX_DISCOUNT_DETAILS.FORMATID='" + ddlFormat.SelectedValue.ToString() + "'";
            //DataTable dtservice = DU.getdata(sql);
            string strsql = "SELECT SERVICEID, SERVICENAME from IVC_SERVICEMASTER";
            DataTable dtService = DU.getdata(strsql);
            if (dtService.Rows.Count > 0)
            {
                ddl.DataTextField = "SERVICENAME";
                ddl.DataValueField = "SERVICEID";
                ddl.DataSource = dtService;
                ddl.DataBind();

                ListItem li = new ListItem("-select service-", "0");
                li.Selected = true;
                ddl.Items.Insert(0, li);
            }
            //else
            //{
            //    ListItem li = new ListItem("-select service-", "0");
            //    li.Selected = true;
            //    ddl.Items.Insert(0, li);
            //}
        }
    }

    protected void ddlservice_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        string ddlid = ddl.ID.ToString().Substring(10);
        int rowno = Convert.ToInt32(ddlid.ToString());
        ((TextBox)tblmain.Rows[rowno].FindControl("txtservicecharge1crtl" + rowno)).Text = string.Empty;
        ((HiddenField)tblmain.Rows[rowno].FindControl("hidServiceCharge" + rowno)).Value = string.Empty;
        ((HiddenField)tblmain.Rows[rowno].FindControl("hidTaxableNonTaxable" + rowno)).Value = string.Empty;
        ((HiddenField)tblmain.Rows[rowno].FindControl("hidTaxableNonTaxable" + rowno)).Value = string.Empty;
        SqlParameter[] parmaservice = { 
                                       new SqlParameter("@serviceid",ddl.SelectedValue)
                                      };
        DataTable dtservice = DU.getdatausingprocedure("USP_IVC_GetAllServices_By_ServiceId", parmaservice);
        if (dtservice.Rows.Count > 0)
        {
            if (Convert.ToString(dtservice.Rows[0]["TAXABILITY"]) == "0" && Convert.ToString(dtservice.Rows[0]["SUBCLASS"]) == "1")
            {
                ((HiddenField)tblmain.Rows[rowno].FindControl("hidTaxableNonTaxable" + rowno)).Value = "Taxable";
            }
            else
            {
                ((HiddenField)tblmain.Rows[rowno].FindControl("hidTaxableNonTaxable" + rowno)).Value = "NonTaxable";
            }
            ((TextBox)tblmain.Rows[rowno].FindControl("txtservicecharge1crtl" + rowno)).Text = String.Format("{0:0.00}", Convert.ToDouble(dtservice.Rows[0]["SERVICECHARGERATE"].ToString()));
            ((HiddenField)tblmain.Rows[rowno].FindControl("hidServiceCharge" + rowno)).Value = String.Format("{0:0.00}", Convert.ToDouble(dtservice.Rows[0]["SERVICECHARGERATE"].ToString())); 
        }
    }

    protected void GetDynamicColumn()
    {
        SqlParameter[] parma = { 
                                new SqlParameter("@Formatid",ddlFormat.SelectedValue)
                               };
        DataTable dtcol = DU.getdatausingprocedure("Usp_GetTransactionFormat_EnableDisable", parma);
        if (dtcol.Rows.Count > 0)
        {
            int count = Convert.ToInt32(hcount.Value.ToString());  // Table Control Count
            for (int i = 1; i <= count; i++)
            {
                hidunit.Value = "";
                hidSaleLevel.Value = "";
                hidTaxType.Value = "";
                hidDiscount.Value = "";
                if (dtcol.Rows[0]["TaxLevel"].ToString() == "True")
                {
                    hidSaleLevel.Value = "ItemLevel";

                        if (dtcol.Rows[0]["ItemTaxType"].ToString() == "0")
                        {
                            hidTaxType.Value = "ST"; // Service Tax
                        }
                        else if (dtcol.Rows[0]["ItemTaxType"].ToString() == "1")
                        {
                            hidTaxType.Value = "STLT"; // Service Tax,ServiceTax,Luxuary Tax
                        }
                        else if (dtcol.Rows[0]["ItemTaxType"].ToString() == "2")
                        {
                            hidTaxType.Value = "STVT"; // Service Tax,VAT
                        }

                        if (dtcol.Rows[0]["DiscountLevel"].ToString() == "0")   // Apply Discount
                        {
                            hidDiscount.Value = "None";
                        }
                        else if (dtcol.Rows[0]["DiscountLevel"].ToString() == "2")
                        {
                            hidDiscount.Value = "ItemLevel";
                        }
 
                }
                else
                {
                    hidSaleLevel.Value = "BillLevel";

                    if (dtcol.Rows[0]["DiscountLevel"].ToString() == "0")   // Apply Discount
                    {
                        hidDiscount.Value = "None";
                    }
                    else if (dtcol.Rows[0]["DiscountLevel"].ToString() == "1")
                    {
                        hidDiscount.Value = "BillLevel";
                    }
                    else if (dtcol.Rows[0]["DiscountLevel"].ToString() == "2")
                    {
                        hidDiscount.Value = "ItemLevel";
                    }
                }
                if (dtcol.Rows[0]["DiscountLevel"].ToString() == "1" && dtcol.Rows[0]["TaxLevel"].ToString() == "True")
                {
                    hidDiscount.Value = "BillLevel";
                }

                if (dtcol.Rows[0]["Adddesc"].ToString() == "True")
                {
                    tddescription.Visible = true;
                    //((HtmlTableCell)tblmain.Rows[i].FindControl("tddescription")).Visible = true;
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tddescriptionctrl" + i.ToString())).Visible = true;
                    ((TextBox)tblmain.Rows[i].FindControl("txtdescriptionctrl" + i.ToString())).Visible = true;
                }
                else
                {
                    tddescription.Visible = false;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tddescriptionctrl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtdescriptionctrl" + i.ToString())).Visible = false;
                }
                if (dtcol.Rows[0]["Yearreference"].ToString() == "True")
                {
                    tdyearref.Visible = true;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdyearrefctrl" + i.ToString())).Visible = true;
                    ((TextBox)tblmain.Rows[i].FindControl("txtyearrefctrl" + i.ToString())).Visible = true;
                }
                else
                {
                    tdyearref.Visible = false;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdyearrefctrl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtyearrefctrl" + i.ToString())).Visible = false;
                }
                if (dtcol.Rows[0]["uom"].ToString() == "True")
                {
                    tduom.Visible = true;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tduomctrl" + i.ToString())).Visible = true;
                    ((TextBox)tblmain.Rows[i].FindControl("txtuomctrl" + i.ToString())).Visible = true;
                }
                else
                {
                    tduom.Visible = false;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tduomctrl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtuomctrl" + i.ToString())).Visible = false;
                }
                if (dtcol.Rows[0]["unit2"].ToString() == "True")
                {
                    tdunit2.Visible = true;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunit2ctrl" + i.ToString())).Visible = true;
                    ((TextBox)tblmain.Rows[i].FindControl("txtunit2ctrl" + i.ToString())).Visible = true;

                    hidunit.Value = "u2";
                }
                else
                {
                    tdunit2.Visible = false;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunit2ctrl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtunit2ctrl" + i.ToString())).Visible = false;
                    hidunit.Value = "";
                }
                
                if (dtcol.Rows[0]["unit3"].ToString() == "True")
                {
                    tdunit3.Visible = true;
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunit3ctrl" + i.ToString())).Visible = true;
                    ((TextBox)tblmain.Rows[i].FindControl("txtunit3ctrl" + i.ToString())).Visible = true;
                    if (hidunit.Value == "u2")
                    {
                        hidunit.Value = "u2u3";
                    }
                    else
                    {
                        hidunit.Value = "u3";
                    }
                }
                else
                {
                    tdunit3.Visible = false;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunit3ctrl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtunit3ctrl" + i.ToString())).Visible = false;
                    if (hidunit.Value != "u2")
                    {
                        hidunit.Value = "";
                    }
                }
                if (dtcol.Rows[0]["unit4"].ToString() == "True")
                {
                    tdunit4.Visible = true;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunit4ctrl" + i.ToString())).Visible = true;
                    ((TextBox)tblmain.Rows[i].FindControl("txtunit4ctrl" + i.ToString())).Visible = true;
                    if (hidunit.Value == "u2u3")
                    {
                        hidunit.Value = "u2u3u4";
                    }
                    else if (hidunit.Value == "u2")
                    {
                        hidunit.Value = "u2u4";
                    }
                    else if (hidunit.Value == "u3")
                    {
                        hidunit.Value = "u3u4";
                    }
                    else
                    {
                        hidunit.Value = "u4";
                    }
                }
                else
                {
                    tdunit4.Visible = false;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunit4ctrl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtunit4ctrl" + i.ToString())).Visible = false;
                    if (hidunit.Value != "u2u3" && hidunit.Value!="u2" && hidunit.Value!="u3")
                    {
                        hidunit.Value = "";
                    }
                }
                hidunitrate.Value = "";
                if (dtcol.Rows[0]["unitrate"].ToString() == "True")
                {
                    tdunitrate1.Visible = true;
                    tdunitrate2.Visible = true;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunitrate1ctrl" + i.ToString())).Visible = true;
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunitrate2ctrl" + i.ToString())).Visible = true;
                    ((TextBox)tblmain.Rows[i].FindControl("txtunitrate1ctrl" + i.ToString())).Visible = true;
                    ((TextBox)tblmain.Rows[i].FindControl("txtunitrate2ctrl" + i.ToString())).Visible = true;
                    hidunitrate.Value = "yes";
                }
                else
                {
                    tdunitrate1.Visible = false;
                    tdunitrate2.Visible = false;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunitrate1ctrl" + i.ToString())).Visible = false;
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunitrate2ctrl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtunitrate1ctrl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtunitrate2ctrl" + i.ToString())).Visible = false;
                    hidunitrate.Value = "No";

                    if (hidunit.Value.Contains("u2"))
                    {
                        tdunitrate2.Visible = true;
                        ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunitrate2ctrl" + i.ToString())).Visible = true;
                        ((TextBox)tblmain.Rows[i].FindControl("txtunitrate2ctrl" + i.ToString())).Visible = true;
                    }
                    if (hidunit.Value.Contains("u3"))
                    {
                        tdunitrate2.Visible = true;
                        ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunitrate2ctrl" + i.ToString())).Visible = true;
                        ((TextBox)tblmain.Rows[i].FindControl("txtunitrate2ctrl" + i.ToString())).Visible = true;
                    }
                    if (hidunit.Value.Contains("u4"))
                    {
                        tdunitrate2.Visible = true;
                        ((HtmlTableCell)tblmain.Rows[i].FindControl("tdunitrate2ctrl" + i.ToString())).Visible = true;
                        ((TextBox)tblmain.Rows[i].FindControl("txtunitrate2ctrl" + i.ToString())).Visible = true;
                    }
                }


                if (dtcol.Rows[0]["DiscountLevel"].ToString() == "1") // Discount Bill Level
                {
                    tddiscount1.Visible = false;
                    tddiscount2.Visible = false;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tddiscount1ctrl" + i.ToString())).Visible = false;
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tddiscount2ctrl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtdiscount1ctrl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtdiscount2ctrl" + i.ToString())).Visible = false;

                    tddiscounttax.Visible=true;
                    tddiscounttaxper.Visible=true;
                    txtdiscounttaxper.Visible=true;
                    tddiscounttaxamt.Visible=true;
                    txtdiscounttaxamt.Visible=true;

                    tddiscountnontax.Visible=true;
                    tdnondiscountper.Visible=true;
                    txtnondiscounttaxper.Visible=true;
                    tdnondiscounttaxamt.Visible=true;
                    txtnondiscounttaxamt.Visible = true;
                }
                else if (dtcol.Rows[0]["Discountlevel"].ToString() == "2")  // Discount Item Level
                {
                    tddiscount1.Visible = true;
                    tddiscount2.Visible = true;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tddiscount1ctrl" + i.ToString())).Visible = true;
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tddiscount2ctrl" + i.ToString())).Visible = true;
                    ((TextBox)tblmain.Rows[i].FindControl("txtdiscount1ctrl" + i.ToString())).Visible = true;
                    ((TextBox)tblmain.Rows[i].FindControl("txtdiscount2ctrl" + i.ToString())).Visible = true;

                    tddiscounttax.Visible = true;
                    tddiscounttaxper.Visible = true;
                    tddiscounttaxamt.Visible = true;
                    txtdiscounttaxamt.Visible = true;
                    tddiscountnontax.Visible = true;
                    tdnondiscountper.Visible = true;
                    tdnondiscounttaxamt.Visible = true;
                    txtnondiscounttaxamt.Visible = true;
                    
                    txtnondiscounttaxper.Visible = false;
                    txtdiscounttaxper.Visible = false;
                  
                }
            else
            {
                tddiscount1.Visible = false;
                tddiscount2.Visible = false;

                ((HtmlTableCell)tblmain.Rows[i].FindControl("tddiscount1ctrl" + i.ToString())).Visible = false;
                ((HtmlTableCell)tblmain.Rows[i].FindControl("tddiscount2ctrl" + i.ToString())).Visible = false;
                ((TextBox)tblmain.Rows[i].FindControl("txtdiscount1ctrl" + i.ToString())).Visible = false;
                ((TextBox)tblmain.Rows[i].FindControl("txtdiscount2ctrl" + i.ToString())).Visible = false;

                tddiscounttax.Visible = false;
                tddiscounttaxper.Visible = false;
                txtdiscounttaxper.Visible = false;
                tddiscounttaxamt.Visible = false;
                txtdiscounttaxamt.Visible = false;

                tddiscountnontax.Visible = false;
                tdnondiscountper.Visible = false;
                txtnondiscounttaxper.Visible = false;
                tdnondiscounttaxamt.Visible = false;
                txtnondiscounttaxamt.Visible = false;

            }
                if (dtcol.Rows[0]["ShowserviceCharge"].ToString() == "True")
                {
                    tdservicecharge1.Visible = true;
                    tdservicecharge2.Visible = true;
                    tdservicecharge3.Visible = true;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdservicecharge1crtl" + i.ToString())).Visible = true;
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdservicecharge2crtl" + i.ToString())).Visible = true;
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdservicecharge3crtl" + i.ToString())).Visible = true;
                    ((TextBox)tblmain.Rows[i].FindControl("txtservicecharge1crtl" + i.ToString())).Visible = true;
                    ((TextBox)tblmain.Rows[i].FindControl("txtservicecharge2crtl" + i.ToString())).Visible = true;
                    ((TextBox)tblmain.Rows[i].FindControl("txtservicecharge3crtl" + i.ToString())).Visible = true;
                     hidsercharge.Value="yes";
                }
                else
                {
                    tdservicecharge1.Visible = false;
                    tdservicecharge2.Visible = false;
                    tdservicecharge3.Visible = false;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdservicecharge1crtl" + i.ToString())).Visible = false;
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdservicecharge2crtl" + i.ToString())).Visible = false;
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdservicecharge3crtl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtservicecharge1crtl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtservicecharge2crtl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtservicecharge3crtl" + i.ToString())).Visible = false;
                    hidsercharge.Value="no";
                }
                if (Convert.ToBoolean(dtcol.Rows[0]["Taxlevel"].ToString()) ==true)
                {

                    tdserviceTax1.Visible = true;
                    tdserviceTax2.Visible = true;
                    tdserviceTax3.Visible = true;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax1crtl" + i.ToString())).Visible = true;
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax2crtl" + i.ToString())).Visible = true;
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax3crtl" + i.ToString())).Visible = true;
                    ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax1crtl" + i.ToString())).Visible = true;
                    ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax2crtl" + i.ToString())).Visible = true;
                    ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax3crtl" + i.ToString())).Visible = true;
                   if (dtcol.Rows[0]["ItemTaxType"].ToString() == "1")
                    {
                        tdserviceTax4.Visible = true;

                        ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax4crtl" + i.ToString())).Visible = true;
                        ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax4crtl" + i.ToString())).Visible = true;
                    }
                    else if (dtcol.Rows[0]["ItemTaxType"].ToString() == "2")
                    {
                        tdserviceTax5.Visible = true;

                        ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax5crtl" + i.ToString())).Visible = true;
                        ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax5crtl" + i.ToString())).Visible = true;
                    }
                }
                else
                {
                    tdserviceTax1.Visible = false;
                    tdserviceTax2.Visible = false;
                    tdserviceTax3.Visible = false;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax1crtl" + i.ToString())).Visible = false;
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax2crtl" + i.ToString())).Visible = false;
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax3crtl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax1crtl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax2crtl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax3crtl" + i.ToString())).Visible = false;

                    tdserviceTax4.Visible = false;
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax4crtl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax4crtl" + i.ToString())).Visible = false;
                    tdserviceTax5.Visible = false;
                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdserviceTax5crtl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtserviceTax5crtl" + i.ToString())).Visible = false;
                }
                if (dtcol.Rows[0]["Taxinclusive"].ToString() == "True" || dtcol.Rows[0]["ItemTaxType"].ToString() == "0")
                {
                    tdTaxincGrandTotal.Visible = true;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdTaxincGrandTotalctrl" + i.ToString())).Visible = true;
                    ((TextBox)tblmain.Rows[i].FindControl("txtTaxincGrandTotalctrl" + i.ToString())).Visible = true;
                    hidGrandTotal.Value = "yes";
                }
                else
                {
                    tdTaxincGrandTotal.Visible = false;

                    ((HtmlTableCell)tblmain.Rows[i].FindControl("tdTaxincGrandTotalctrl" + i.ToString())).Visible = false;
                    ((TextBox)tblmain.Rows[i].FindControl("txtTaxincGrandTotalctrl" + i.ToString())).Visible = false;
                    hidGrandTotal.Value = "no";
                }
                //tblmain.Rows.Add(row1);
                //}
            }

        }
    }

    protected void AddMore_Click(Object sender, EventArgs e)
    {
        ADDMoreRow();
        GetDynamicColumn();
    }

    protected void ADDMoreRow()
    {
        counter++;
        listcounter.Add(counter.ToString());

        HtmlTableRow row = new HtmlTableRow();

        HtmlTableCell cell1 = new HtmlTableCell();
        cell1.ID = "tdservicectrl" + counter.ToString();
        cell1.Align = "Center";
        listtdservicectrl.Add(cell1.ID.ToString());

        DropDownList ddlservice = new DropDownList();
        ddlservice.ID = "ddlservice" + counter.ToString();
        ddlservice.CssClass = "dropdown";
        ddlservice.Width = Unit.Pixel(150);
        ddlservice.AutoPostBack = true;
        BindService(ddlservice);
        ddlservice.Attributes.Add("onchange", "return ValidateRequired(this.id);");
        ddlservice.SelectedIndexChanged += new EventHandler(ddlservice_SelectedIndexChanged);

        listddlservice.Add(ddlservice.ID.ToString());
       
        HiddenField hidTaxableNonTaxable = new HiddenField();
        hidTaxableNonTaxable.ID = "hidTaxableNonTaxable" + counter.ToString();
        listhidTaxableNonTaxable.Add(hidTaxableNonTaxable.ID.ToString());

        HtmlTableCell cell2 = new HtmlTableCell();
        cell2.ID = "tddescriptionctrl"+counter.ToString();
        cell2.Align = "Center";
        cell2.Visible = false;
        listtddescriptionctrl.Add(cell2.ID.ToString());

        TextBox txtdescriptionctrl = new TextBox();
        txtdescriptionctrl.ID = "txtdescriptionctrl" + counter.ToString();
        txtdescriptionctrl.Visible = false;
        txtdescriptionctrl.CssClass = "input";
        listtxtdescriptionctrl.Add(txtdescriptionctrl.ID.ToString());

        HtmlTableCell cell3 = new HtmlTableCell();
        cell3.ID = "tdyearrefctrl" + counter.ToString();
        cell3.Align = "Center";
        cell3.Visible = false;
        listtdyearrefctrl.Add(cell3.ID.ToString());

        TextBox txtyearrefctrl = new TextBox();
        txtyearrefctrl.ID = "txtyearrefctrl" + counter.ToString();
        txtyearrefctrl.Width = Unit.Pixel(70);
        txtyearrefctrl.Visible = false;
        txtyearrefctrl.CssClass = "input";
        listtxtyearrefctrl.Add(txtyearrefctrl.ID.ToString());

        HtmlTableCell cell4 = new HtmlTableCell();
        cell4.ID = "tduomctrl" + counter.ToString();
        cell4.Align = "Center";
        cell4.Visible = false;
        listtduomctrl.Add(cell4.ID.ToString());

        TextBox txtuomctrl = new TextBox();
        txtuomctrl.ID = "txtuomctrl" + counter.ToString();
        //txtuomctrl.Width = Unit.Pixel(70);
        txtuomctrl.Visible = false;
        txtuomctrl.CssClass = "input";
        listtxtuomctrl.Add(txtuomctrl.ID.ToString());

        HtmlTableCell cell5 = new HtmlTableCell();
        cell5.ID = "tdunit2ctrl" + counter.ToString();
        cell5.Align = "Center";
        cell5.Visible = false;
        listtdunit2ctrl.Add(cell5.ID.ToString());

        TextBox txtunit2ctrl = new TextBox();
        txtunit2ctrl.ID = "txtunit2ctrl" + counter.ToString();
        txtunit2ctrl.Width = Unit.Pixel(70);
        txtunit2ctrl.Visible = false;
        txtunit2ctrl.CssClass = "inputnumber";
        txtunit2ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.ID,event);");
        listtxtunit2ctrl.Add(txtunit2ctrl.ID.ToString());

        HtmlTableCell cell6 = new HtmlTableCell();
        cell6.ID = "tdunit3ctrl" + counter.ToString();
        cell6.Align = "Center";
        cell6.Visible = false;
        listtdunit3ctrl.Add(cell6.ID.ToString());

        TextBox txtunit3ctrl = new TextBox();
        txtunit3ctrl.ID = "txtunit3ctrl" + counter.ToString();
        txtunit3ctrl.Width = Unit.Pixel(70);
        txtunit3ctrl.Visible = false;
        txtunit3ctrl.CssClass = "inputnumber";
        txtunit3ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.ID,event);");
        listtxtunit3ctrl.Add(txtunit3ctrl.ID.ToString());


        HtmlTableCell cell7 = new HtmlTableCell();
        cell7.ID = "tdunit4ctrl" + counter.ToString();
        cell7.Align = "Center";
        cell7.Visible = false;
        listtdunit4ctrl.Add(cell7.ID.ToString());

        TextBox txtunit4ctrl = new TextBox();
        txtunit4ctrl.ID = "txtunit4ctrl" + counter.ToString();
        txtunit4ctrl.Width = Unit.Pixel(70);
        txtunit4ctrl.Visible = false;
        txtunit4ctrl.CssClass = "inputnumber";
        txtunit4ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.ID,event);");
        listtxtunit4ctrl.Add(txtunit4ctrl.ID.ToString());

        HtmlTableCell cell8 = new HtmlTableCell();
        cell8.ID = "tdunitrate1ctrl" + counter.ToString();
        cell8.Align = "Center";
        cell8.Visible = false;
        listtdunitrate1ctrl.Add(cell8.ID.ToString());

        TextBox txtunitrate1ctrl = new TextBox();
        txtunitrate1ctrl.ID = "txtunitrate1ctrl" + counter.ToString();
        txtunitrate1ctrl.Width = Unit.Pixel(70);
        txtunitrate1ctrl.Visible = false;
        txtunitrate1ctrl.CssClass = "inputnumber";
        txtunitrate1ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.ID,event);");
        listtxtunitrate1ctrl.Add(txtunitrate1ctrl.ID.ToString());

        HtmlTableCell cell9 = new HtmlTableCell();
        cell9.ID = "tdunitrate2ctrl" + counter.ToString();
        cell9.Align = "Center";
        cell9.Visible = false;
        listtdunitrate2ctrl.Add(cell9.ID.ToString());

        TextBox txtunitrate2ctrl = new TextBox();
        txtunitrate2ctrl.ID = "txtunitrate2ctrl" + counter.ToString();
        txtunitrate2ctrl.Width = Unit.Pixel(70);
        txtunitrate2ctrl.Visible = false;
        txtunitrate2ctrl.CssClass = "DisableInputNumber";
        txtunitrate2ctrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");
        listtxtunitrate2ctrl.Add(txtunitrate2ctrl.ID.ToString());

        HtmlTableCell cell10 = new HtmlTableCell();
        cell10.ID = "tdgrossamtctrl" + counter.ToString();
        cell10.Align = "Center";
        //cell10.Visible = false;
        listtdgrossamtctrl.Add(cell10.ID.ToString());

        TextBox txtgrossamtctrl = new TextBox();
        txtgrossamtctrl.ID = "txtgrossamtctrl" + counter.ToString();
        txtgrossamtctrl.Width = Unit.Pixel(120);
        //txtgrossamtctrl.Visible = false;
        txtgrossamtctrl.CssClass = "DisableInputNumber";
        txtgrossamtctrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");
        listtxtgrossamtctrl.Add(txtgrossamtctrl.ID.ToString());

        HiddenField hidServiceCharge = new HiddenField();
        hidServiceCharge.ID = "hidServiceCharge" + counter.ToString();
        listhidServiceCharge.Add(hidServiceCharge.ID.ToString());

        HtmlTableCell cell11 = new HtmlTableCell();
        cell11.ID = "tddiscount1ctrl" + counter.ToString();
        cell11.Align = "Center";
        cell11.Visible = false;
        listtddiscount1ctrl.Add(cell11.ID.ToString());

        TextBox txtdiscount1ctrl = new TextBox();
        txtdiscount1ctrl.ID = "txtdiscount1ctrl" + counter.ToString();
        txtdiscount1ctrl.Width = Unit.Pixel(80);
        txtdiscount1ctrl.Visible = false;
        txtdiscount1ctrl.CssClass = "inputnumber";
        txtdiscount1ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.ID,event);");
        listtxtdiscount1ctrl.Add(txtdiscount1ctrl.ID.ToString());

        HtmlTableCell cell12 = new HtmlTableCell();
        cell12.ID = "tddiscount2ctrl" + counter.ToString();
        cell12.Align = "Center";
        cell12.Visible = false;
        listtddiscount2ctrl.Add(cell12.ID.ToString());

        TextBox txtdiscount2ctrl = new TextBox();
        txtdiscount2ctrl.ID = "txtdiscount2ctrl" + counter.ToString();
        txtdiscount2ctrl.Width = Unit.Pixel(120);
        txtdiscount2ctrl.Visible = false;
        txtdiscount2ctrl.CssClass = "DisableInputNumber";
        txtdiscount2ctrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");
        listtxtdiscount2ctrl.Add(txtdiscount2ctrl.ID.ToString());

        HtmlTableCell cell13 = new HtmlTableCell();
        cell13.ID = "tdservicecharge1crtl" + counter.ToString();
        cell13.Align = "Center";
        cell13.Visible = false;
        listtdservicecharge1crtl.Add(cell13.ID.ToString());

        TextBox txtservicecharge1crtl = new TextBox();
        txtservicecharge1crtl.ID = "txtservicecharge1crtl" + counter.ToString();
        txtservicecharge1crtl.Width = Unit.Pixel(120);
        txtservicecharge1crtl.Visible = false;
        txtservicecharge1crtl.CssClass = "inputnumber";
        txtservicecharge1crtl.Attributes.Add("OnKeyPress", "return isNumberKey(this.ID,event);");
        txtservicecharge1crtl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");
        listtxtservicecharge1crtl.Add(txtservicecharge1crtl.ID.ToString());

        HtmlTableCell cell14 = new HtmlTableCell();
        cell14.ID = "tdservicecharge2crtl" + counter.ToString();
        cell14.Align = "Center";
        cell14.Visible = false;
        listtdservicecharge2crtl.Add(cell14.ID.ToString());

        TextBox txtservicecharge2crtl = new TextBox();
        txtservicecharge2crtl.ID = "txtservicecharge2crtl" + counter.ToString();
        txtservicecharge2crtl.Width = Unit.Pixel(100);
        txtservicecharge2crtl.Visible = false;
        txtservicecharge2crtl.CssClass = "DisableInputNumber";
        txtservicecharge2crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");
        listtxtservicecharge2crtl.Add(txtservicecharge2crtl.ID.ToString());

        HtmlTableCell cell15 = new HtmlTableCell();
        cell15.ID = "tdservicecharge3crtl" + counter.ToString();
        cell15.Align = "Center";
        cell15.Visible = false;
        listtdservicecharge3crtl.Add(cell15.ID.ToString());

        TextBox txtservicecharge3crtl = new TextBox();
        txtservicecharge3crtl.ID = "txtservicecharge3crtl" + counter.ToString();
        txtservicecharge3crtl.Width = Unit.Pixel(100);
        txtservicecharge3crtl.Visible = false;
        txtservicecharge3crtl.CssClass = "DisableInputNumber";
        txtservicecharge3crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");
        listtxtservicecharge3crtl.Add(txtservicecharge3crtl.ID.ToString());

        HtmlTableCell cell16 = new HtmlTableCell();
        cell16.ID = "tdserviceTax1crtl" + counter.ToString();
        cell16.Align = "Center";
        cell16.Visible = false;
        listtdserviceTax1crtl.Add(cell16.ID.ToString());

        TextBox txtserviceTax1crtl = new TextBox();
        txtserviceTax1crtl.ID = "txtserviceTax1crtl" + counter.ToString();
        txtserviceTax1crtl.Width = Unit.Pixel(100);
        txtserviceTax1crtl.Visible = false;
        txtserviceTax1crtl.CssClass = "DisableInputNumber";
        txtserviceTax1crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");
        listtxtserviceTax1crtl.Add(txtserviceTax1crtl.ID.ToString());

        HtmlTableCell cell17 = new HtmlTableCell();
        cell17.ID = "tdserviceTax2crtl" + counter.ToString();
        cell17.Align = "Center";
        cell17.Visible = false;
        listtdserviceTax2crtl.Add(cell17.ID.ToString());

        TextBox txtserviceTax2crtl = new TextBox();
        txtserviceTax2crtl.ID = "txtserviceTax2crtl" + counter.ToString();
        txtserviceTax2crtl.Width = Unit.Pixel(70);
        txtserviceTax2crtl.Visible = false;
        txtserviceTax2crtl.CssClass = "DisableInputNumber";
        txtserviceTax2crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");
        listtxtserviceTax2crtl.Add(txtserviceTax2crtl.ID.ToString());

        HtmlTableCell cell18 = new HtmlTableCell();
        cell18.ID = "tdserviceTax3crtl" + counter.ToString();
        cell18.Align = "Center";
        cell18.Visible = false;
        listtdserviceTax3crtl.Add(cell18.ID.ToString());

        TextBox txtserviceTax3crtl = new TextBox();
        txtserviceTax3crtl.ID = "txtserviceTax3crtl" + counter.ToString();
        txtserviceTax3crtl.Width = Unit.Pixel(70);
        txtserviceTax3crtl.Visible = false;
        txtserviceTax3crtl.CssClass = "DisableInputNumber";
        txtserviceTax3crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");
        listtxtserviceTax3crtl.Add(txtserviceTax3crtl.ID.ToString());

        HtmlTableCell cell19 = new HtmlTableCell();
        cell19.ID = "tdserviceTax4crtl" + counter.ToString();
        cell19.Align = "Center";
        cell19.Visible = false;
        listtdserviceTax4crtl.Add(cell19.ID.ToString());

        TextBox txtserviceTax4crtl = new TextBox();
        txtserviceTax4crtl.ID = "txtserviceTax4crtl" + counter.ToString();
        txtserviceTax4crtl.Width = Unit.Pixel(70);
        txtserviceTax4crtl.Visible = false;
        txtserviceTax4crtl.CssClass = "DisableInputNumber";
        txtserviceTax4crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");
        listtxtserviceTax4crtl.Add(txtserviceTax4crtl.ID.ToString());

        HtmlTableCell cell20 = new HtmlTableCell();
        cell20.ID = "tdserviceTax5crtl" + counter.ToString();
        cell20.Align = "Center";
        cell20.Visible = false;
        listtdserviceTax5crtl.Add(cell20.ID.ToString());

        TextBox txtserviceTax5crtl = new TextBox();
        txtserviceTax5crtl.ID = "txtserviceTax5crtl" + counter.ToString();
        txtserviceTax5crtl.Width = Unit.Pixel(70);
        txtserviceTax5crtl.Visible = false;
        txtserviceTax5crtl.CssClass = "DisableInputNumber";
        txtserviceTax5crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");
        listtxtserviceTax5crtl.Add(txtserviceTax5crtl.ID.ToString());


        HtmlTableCell cell21 = new HtmlTableCell();
        cell21.ID = "tdTaxincGrandTotalctrl" + counter.ToString();
        cell21.Align = "Center";
        cell21.Visible = false;
        listtdTaxincGrandTotalctrl.Add(cell21.ID.ToString());

        TextBox txtTaxincGrandTotalctrl = new TextBox();
        txtTaxincGrandTotalctrl.ID = "txtTaxincGrandTotalctrl" + counter.ToString();
        txtTaxincGrandTotalctrl.Width = Unit.Pixel(120);
        txtTaxincGrandTotalctrl.Visible = false;
        txtTaxincGrandTotalctrl.CssClass = "inputnumber";
        txtTaxincGrandTotalctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.ID,event);");
        txtTaxincGrandTotalctrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");
        listtxtTaxincGrandTotalctrl.Add(txtTaxincGrandTotalctrl.ID.ToString());

        cell1.Controls.Add(ddlservice);
        cell1.Controls.Add(hidTaxableNonTaxable);
        cell2.Controls.Add(txtdescriptionctrl);
        cell3.Controls.Add(txtyearrefctrl);
        cell4.Controls.Add(txtuomctrl);
        cell5.Controls.Add(txtunit2ctrl);
        cell6.Controls.Add(txtunit3ctrl);
        cell7.Controls.Add(txtunit4ctrl);
        cell8.Controls.Add(txtunitrate1ctrl);
        cell9.Controls.Add(txtunitrate2ctrl);
        cell10.Controls.Add(txtgrossamtctrl);
        cell10.Controls.Add(hidServiceCharge);
        cell11.Controls.Add(txtdiscount1ctrl);
        cell12.Controls.Add(txtdiscount2ctrl);
        cell13.Controls.Add(txtservicecharge1crtl);
        cell14.Controls.Add(txtservicecharge2crtl);
        cell15.Controls.Add(txtservicecharge3crtl);
        cell16.Controls.Add(txtserviceTax1crtl);
        cell17.Controls.Add(txtserviceTax2crtl);
        cell18.Controls.Add(txtserviceTax3crtl);
        cell19.Controls.Add(txtserviceTax4crtl);
        cell20.Controls.Add(txtserviceTax5crtl);
        cell21.Controls.Add(txtTaxincGrandTotalctrl);

        row.Cells.Add(cell1);
        row.Cells.Add(cell2);
        row.Cells.Add(cell3);
        row.Cells.Add(cell4);
        row.Cells.Add(cell5);
        row.Cells.Add(cell6);
        row.Cells.Add(cell7);
        row.Cells.Add(cell8);
        row.Cells.Add(cell9);
        row.Cells.Add(cell10);
        row.Cells.Add(cell11);
        row.Cells.Add(cell12);
        row.Cells.Add(cell13);
        row.Cells.Add(cell14);
        row.Cells.Add(cell15);
        row.Cells.Add(cell16);
        row.Cells.Add(cell17);
        row.Cells.Add(cell18);
        row.Cells.Add(cell19);
        row.Cells.Add(cell20);
        row.Cells.Add(cell21);

        tblmain.Rows.Add(row);

        hcount.Value = counter.ToString();

        ViewState["listcounter"] = listcounter;

        ViewState["listtdservicectrl"] = listtdservicectrl;
        ViewState["listddlservice"] = listddlservice;
        ViewState["listhidTaxableNonTaxable"] = listhidTaxableNonTaxable;

        ViewState["listtddescriptionctrl"] = listtddescriptionctrl;
        ViewState["listtxtdescriptionctrl"] = listtxtdescriptionctrl;
      
        ViewState["listtdyearrefctrl"] = listtdyearrefctrl;
        ViewState["listtxtyearrefctrl"] = listtxtyearrefctrl;

        ViewState["listtduomctrl"] = listtduomctrl;
        ViewState["listtxtuomctrl"] = listtxtuomctrl;

        ViewState["listtdunit2ctrl"] = listtdunit2ctrl;
        ViewState["listtxtunit2ctrl"] = listtxtunit2ctrl;
        ViewState["listtdunit3ctrl"] = listtdunit3ctrl;
        ViewState["listtxtunit3ctrl"] = listtxtunit3ctrl;
        ViewState["listtdunit4ctrl"] = listtdunit4ctrl;
        ViewState["listtxtunit4ctrl"] = listtxtunit4ctrl;
 
        ViewState["listtdunitrate1ctrl"] = listtdunitrate1ctrl;
        ViewState["listtxtunitrate1ctrl"] = listtxtunitrate1ctrl;
        ViewState["listtdunitrate2ctrl"] = listtdunitrate2ctrl;
        ViewState["listtxtunitrate2ctrl"] = listtxtunitrate2ctrl;
      
        ViewState["listtdgrossamtctrl"] = listtdgrossamtctrl;
        ViewState["listtxtgrossamtctrl"] = listtxtgrossamtctrl;
        
        ViewState["listtddiscount1ctrl"] = listtddiscount1ctrl;
        ViewState["listtxtdiscount1ctrl"] = listtxtdiscount1ctrl;
       
        ViewState["listtddiscount2ctrl"] = listtddiscount2ctrl;
        ViewState["listtxtdiscount2ctrl"] = listtxtdiscount2ctrl;
       
        ViewState["listtdservicecharge1crtl"] = listtdservicecharge1crtl;
        ViewState["listhidServiceCharge"] = listhidServiceCharge;
        ViewState["listtxtservicecharge1crtl"] = listtxtservicecharge1crtl;
        ViewState["listtdservicecharge2crtl"] = listtdservicecharge2crtl;
        ViewState["listtxtservicecharge2crtl"] = listtxtservicecharge2crtl;
        ViewState["listtdservicecharge3crtl"] = listtdservicecharge3crtl;
        ViewState["listtxtservicecharge3crtl"] = listtxtservicecharge3crtl;

        ViewState["listtdserviceTax1crtl"] = listtdserviceTax1crtl;
        ViewState["listtxtserviceTax1crtl"] = listtxtserviceTax1crtl;
        ViewState["listtdserviceTax2crtl"] = listtdserviceTax2crtl;
        ViewState["listtxtserviceTax2crtl"] = listtxtserviceTax2crtl;
        ViewState["listtdserviceTax3crtl"] = listtdserviceTax3crtl;
        ViewState["listtxtserviceTax3crtl"] = listtxtserviceTax3crtl;
        ViewState["listtdserviceTax4crtl"] = listtdserviceTax4crtl;
        ViewState["listtxtserviceTax4crtl"] = listtxtserviceTax4crtl;
        ViewState["listtdserviceTax5crtl"] = listtdserviceTax5crtl;
        ViewState["listtxtserviceTax5crtl"] = listtxtserviceTax5crtl;
     
        ViewState["listtdTaxincGrandTotalctrl"] = listtdTaxincGrandTotalctrl;
        ViewState["listtxtTaxincGrandTotalctrl"] = listtxtTaxincGrandTotalctrl;

    }
    protected void OverrideAddMore()
    {

        listcounter =(List<string>) ViewState["listcounter"];

        listtdservicectrl = (List<string>)ViewState["listtdservicectrl"];
        listddlservice = (List<string>)ViewState["listddlservice"];

        listhidTaxableNonTaxable = (List<string>)ViewState["listhidTaxableNonTaxable"];

        listtddescriptionctrl = (List<string>)ViewState["listtddescriptionctrl"];
        listtxtdescriptionctrl = (List<string>)ViewState["listtxtdescriptionctrl"];

        listtdyearrefctrl = (List<string>)ViewState["listtdyearrefctrl"];
        listtxtyearrefctrl = (List<string>)ViewState["listtxtyearrefctrl"];

        listtduomctrl = (List<string>)ViewState["listtduomctrl"];
        listtxtuomctrl = (List<string>)ViewState["listtxtuomctrl"];

        listtdunit2ctrl = (List<string>)ViewState["listtdunit2ctrl"];
        listtxtunit2ctrl = (List<string>)ViewState["listtxtunit2ctrl"];
        listtdunit3ctrl = (List<string>)ViewState["listtdunit3ctrl"];
        listtxtunit3ctrl = (List<string>)ViewState["listtxtunit3ctrl"];
        listtdunit4ctrl = (List<string>)ViewState["listtdunit4ctrl"];
        listtxtunit4ctrl = (List<string>)ViewState["listtxtunit4ctrl"];

        listtdunitrate1ctrl = (List<string>)ViewState["listtdunitrate1ctrl"];
        listtxtunitrate1ctrl = (List<string>)ViewState["listtxtunitrate1ctrl"];
        listtdunitrate2ctrl = (List<string>)ViewState["listtdunitrate2ctrl"];
        listtxtunitrate2ctrl = (List<string>)ViewState["listtxtunitrate2ctrl"];

        listtdgrossamtctrl = (List<string>)ViewState["listtdgrossamtctrl"];
        listtxtgrossamtctrl = (List<string>)ViewState["listtxtgrossamtctrl"];

        listtddiscount1ctrl = (List<string>)ViewState["listtddiscount1ctrl"];
        listtxtdiscount1ctrl = (List<string>)ViewState["listtxtdiscount1ctrl"];

        listtddiscount2ctrl = (List<string>)ViewState["listtddiscount2ctrl"];
        listtxtdiscount2ctrl = (List<string>)ViewState["listtxtdiscount2ctrl"];

        listtdservicecharge1crtl = (List<string>)ViewState["listtdservicecharge1crtl"];
        listhidServiceCharge = (List<string>)ViewState["listhidServiceCharge"];
        listtxtservicecharge1crtl = (List<string>)ViewState["listtxtservicecharge1crtl"];
        listtdservicecharge2crtl = (List<string>)ViewState["listtdservicecharge2crtl"];
        listtxtservicecharge2crtl = (List<string>)ViewState["listtxtservicecharge2crtl"];
        listtdservicecharge3crtl = (List<string>)ViewState["listtdservicecharge3crtl"];
        listtxtservicecharge3crtl = (List<string>)ViewState["listtxtservicecharge3crtl"];

        listtdserviceTax1crtl = (List<string>)ViewState["listtdserviceTax1crtl"];
        listtxtserviceTax1crtl = (List<string>)ViewState["listtxtserviceTax1crtl"];
        listtdserviceTax2crtl = (List<string>)ViewState["listtdserviceTax2crtl"];
        listtxtserviceTax2crtl = (List<string>)ViewState["listtxtserviceTax2crtl"];
        listtdserviceTax3crtl = (List<string>)ViewState["listtdserviceTax3crtl"];
        listtxtserviceTax3crtl = (List<string>)ViewState["listtxtserviceTax3crtl"];
        listtdserviceTax4crtl = (List<string>)ViewState["listtdserviceTax4crtl"];
        listtxtserviceTax4crtl = (List<string>)ViewState["listtxtserviceTax4crtl"];
        listtdserviceTax5crtl = (List<string>)ViewState["listtdserviceTax5crtl"];
        listtxtserviceTax5crtl = (List<string>)ViewState["listtxtserviceTax5crtl"];

        listtdTaxincGrandTotalctrl = (List<string>)ViewState["listtdTaxincGrandTotalctrl"];
        listtxtTaxincGrandTotalctrl = (List<string>)ViewState["listtxtTaxincGrandTotalctrl"];

        if (listcounter != null)
        {
            int tempcounter = Convert.ToInt32(listcounter.Count);
            for (int i = 0; i < tempcounter; i++)
            {
                counter++;
           //     listcounter.Add(counter.ToString());

                HtmlTableRow row = new HtmlTableRow();

                HtmlTableCell cell1 = new HtmlTableCell();
                cell1.ID = listtdservicectrl[i].ToString();
                cell1.Align = "Center";
               
                DropDownList ddlservice = new DropDownList();
                ddlservice.ID = listddlservice[i].ToString();
                ddlservice.CssClass = "dropdown";
                ddlservice.Width = Unit.Pixel(150);
                BindService(ddlservice);
                ddlservice.AutoPostBack = true;
                ddlservice.Attributes.Add("onchange", "return ValidateRequired(this.id);");
                ddlservice.SelectedIndexChanged += new EventHandler(ddlservice_SelectedIndexChanged);

                HiddenField hidTaxableNonTaxable = new HiddenField();
                hidTaxableNonTaxable.ID = listhidTaxableNonTaxable[i].ToString();

                HtmlTableCell cell2 = new HtmlTableCell();
                cell2.ID = listtddescriptionctrl[i].ToString();
                cell2.Align = "Center";
                cell2.Visible = false;
                

                TextBox txtdescriptionctrl = new TextBox();
                txtdescriptionctrl.ID = listtxtdescriptionctrl[i].ToString();
                txtdescriptionctrl.Visible = false;
                txtdescriptionctrl.CssClass = "input";

                HtmlTableCell cell3 = new HtmlTableCell();
                cell3.ID = listtdyearrefctrl[i].ToString();
                cell3.Align = "Center";
                cell3.Visible = false;

                TextBox txtyearrefctrl = new TextBox();
                txtyearrefctrl.ID = listtxtyearrefctrl[i].ToString();
                txtyearrefctrl.Width = Unit.Pixel(70);
                txtyearrefctrl.Visible = false;
                txtyearrefctrl.CssClass = "input";

                HtmlTableCell cell4 = new HtmlTableCell();
                cell4.ID = listtduomctrl[i].ToString();
                cell4.Align = "Center";
                cell4.Visible = false;

                TextBox txtuomctrl = new TextBox();
                txtuomctrl.ID = listtxtuomctrl[i].ToString();
                //txtuomctrl.Width = Unit.Pixel(70);
                txtuomctrl.Visible = false;
                txtuomctrl.CssClass = "input";
                
                HtmlTableCell cell5 = new HtmlTableCell();
                cell5.ID = listtdunit2ctrl[i].ToString();
                cell5.Align = "Center";
                cell5.Visible = false;

                TextBox txtunit2ctrl = new TextBox();
                txtunit2ctrl.ID = listtxtunit2ctrl[i].ToString();
                txtunit2ctrl.Width = Unit.Pixel(70);
                txtunit2ctrl.Visible = false;
                txtunit2ctrl.CssClass = "inputnumber";
                txtunit2ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.ID,event);");

                HtmlTableCell cell6 = new HtmlTableCell();
                cell6.ID = listtdunit3ctrl[i].ToString();
                cell6.Align = "Center";
                cell6.Visible = false;

                TextBox txtunit3ctrl = new TextBox();
                txtunit3ctrl.ID = listtxtunit3ctrl[i].ToString();
                txtunit3ctrl.Width = Unit.Pixel(70);
                txtunit3ctrl.Visible = false;
                txtunit3ctrl.CssClass = "inputnumber";
                txtunit3ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.ID,event);");

                HtmlTableCell cell7 = new HtmlTableCell();
                cell7.ID = listtdunit4ctrl[i].ToString();
                cell7.Align = "Center";
                cell7.Visible = false;

                TextBox txtunit4ctrl = new TextBox();
                txtunit4ctrl.ID = listtxtunit4ctrl[i].ToString();
                txtunit4ctrl.Width = Unit.Pixel(70);
                txtunit4ctrl.Visible = false;
                txtunit4ctrl.CssClass = "inputnumber";
                txtunit4ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.ID,event);");

                HtmlTableCell cell8 = new HtmlTableCell();
                cell8.ID = listtdunitrate1ctrl[i].ToString();
                cell8.Align = "Center";
                cell8.Visible = false;

                TextBox txtunitrate1ctrl = new TextBox();
                txtunitrate1ctrl.ID = listtxtunitrate1ctrl[i].ToString();
                txtunitrate1ctrl.Width = Unit.Pixel(70);
                txtunitrate1ctrl.Visible = false;
                txtunitrate1ctrl.CssClass = "inputnumber";
                txtunitrate1ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.ID,event);");

                HtmlTableCell cell9 = new HtmlTableCell();
                cell9.ID = listtdunitrate2ctrl[i].ToString();
                cell9.Align = "Center";
                cell9.Visible = false;

                TextBox txtunitrate2ctrl = new TextBox();
                txtunitrate2ctrl.ID = listtxtunitrate2ctrl[i].ToString();
                txtunitrate2ctrl.Width = Unit.Pixel(70);
                txtunitrate2ctrl.Visible = false;
                txtunitrate2ctrl.CssClass = "DisableInputNumber";
                txtunitrate2ctrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");

                HtmlTableCell cell10 = new HtmlTableCell();
                cell10.ID = listtdgrossamtctrl[i].ToString();
                cell10.Align = "Center";
                //cell10.Visible = false;

                TextBox txtgrossamtctrl = new TextBox();
                txtgrossamtctrl.ID = listtxtgrossamtctrl[i].ToString();
                txtgrossamtctrl.Width = Unit.Pixel(120);
                //txtgrossamtctrl.Visible = false;
                txtgrossamtctrl.CssClass = "DisableInputNumber";
                txtgrossamtctrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");

                HiddenField hidServiceCharge = new HiddenField();
                hidServiceCharge.ID = listhidServiceCharge[i].ToString();

                HtmlTableCell cell11 = new HtmlTableCell();
                cell11.ID = listtddiscount1ctrl[i].ToString();
                cell11.Align = "Center";
                cell11.Visible = false;

                TextBox txtdiscount1ctrl = new TextBox();
                txtdiscount1ctrl.ID = listtxtdiscount1ctrl[i].ToString();
                txtdiscount1ctrl.Width = Unit.Pixel(80);
                txtdiscount1ctrl.Visible = false;
                txtdiscount1ctrl.CssClass = "inputnumber";
                txtdiscount1ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.ID,event);");

                HtmlTableCell cell12 = new HtmlTableCell();
                cell12.ID = listtddiscount2ctrl[i].ToString();
                cell12.Align = "Center";
                cell12.Visible = false;

                TextBox txtdiscount2ctrl = new TextBox();
                txtdiscount2ctrl.ID = listtxtdiscount2ctrl[i].ToString();
                txtdiscount2ctrl.Width = Unit.Pixel(120);
                txtdiscount2ctrl.Visible = false;
                txtdiscount2ctrl.CssClass = "DisableInputNumber";
                txtdiscount2ctrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");

                HtmlTableCell cell13 = new HtmlTableCell();
                cell13.ID = listtdservicecharge1crtl[i].ToString();
                cell13.Align = "Center";
                cell13.Visible = false;

                TextBox txtservicecharge1crtl = new TextBox();
                txtservicecharge1crtl.ID = listtxtservicecharge1crtl[i].ToString();
                txtservicecharge1crtl.Width = Unit.Pixel(120);
                txtservicecharge1crtl.Visible = false;
                txtservicecharge1crtl.CssClass = "inputnumber";
                txtservicecharge1crtl.Attributes.Add("OnKeyPress", "return isNumberKey(this.ID,event);");

                

                HtmlTableCell cell14 = new HtmlTableCell();
                cell14.ID = listtdservicecharge2crtl[i].ToString();
                cell14.Align = "Center";
                cell14.Visible = false;

                TextBox txtservicecharge2crtl = new TextBox();
                txtservicecharge2crtl.ID = listtxtservicecharge2crtl[i].ToString();
                txtservicecharge2crtl.Width = Unit.Pixel(100);
                txtservicecharge2crtl.Visible = false;
                txtservicecharge2crtl.CssClass = "DisableInputNumber";
                txtservicecharge2crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");

                HtmlTableCell cell15 = new HtmlTableCell();
                cell15.ID = listtdservicecharge3crtl[i].ToString();
                cell15.Align = "Center";
                cell15.Visible = false;

                TextBox txtservicecharge3crtl = new TextBox();
                txtservicecharge3crtl.ID = listtxtservicecharge3crtl[i].ToString();
                txtservicecharge3crtl.Width = Unit.Pixel(100);
                txtservicecharge3crtl.Visible = false;
                txtservicecharge3crtl.CssClass = "DisableInputNumber";
                txtservicecharge3crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");

                HtmlTableCell cell16 = new HtmlTableCell();
                cell16.ID = listtdserviceTax1crtl[i].ToString();
                cell16.Align = "Center";
                cell16.Visible = false;

                TextBox txtserviceTax1crtl = new TextBox();
                txtserviceTax1crtl.ID = listtxtserviceTax1crtl[i].ToString();
                txtserviceTax1crtl.Width = Unit.Pixel(100);
                txtserviceTax1crtl.Visible = false;
                txtserviceTax1crtl.CssClass = "DisableInputNumber";
                txtserviceTax1crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");

                HtmlTableCell cell17 = new HtmlTableCell();
                cell17.ID = listtdserviceTax2crtl[i].ToString();
                cell17.Align = "Center";
                cell17.Visible = false;

                TextBox txtserviceTax2crtl = new TextBox();
                txtserviceTax2crtl.ID = listtxtserviceTax2crtl[i].ToString();
                txtserviceTax2crtl.Width = Unit.Pixel(70);
                txtserviceTax2crtl.Visible = false;
                txtserviceTax2crtl.CssClass = "DisableInputNumber";
                txtserviceTax2crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");

                HtmlTableCell cell18 = new HtmlTableCell();
                cell18.ID = listtdserviceTax3crtl[i].ToString();
                cell18.Align = "Center";
                cell18.Visible = false;

                TextBox txtserviceTax3crtl = new TextBox();
                txtserviceTax3crtl.ID = listtxtserviceTax3crtl[i].ToString();
                txtserviceTax3crtl.Width = Unit.Pixel(70);
                txtserviceTax3crtl.Visible = false;
                txtserviceTax3crtl.CssClass = "DisableInputNumber";
                txtserviceTax3crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");

                HtmlTableCell cell19 = new HtmlTableCell();
                cell19.ID = listtdserviceTax4crtl[i].ToString();
                cell19.Align = "Center";
                cell19.Visible = false;

                TextBox txtserviceTax4crtl = new TextBox();
                txtserviceTax4crtl.ID = listtxtserviceTax4crtl[i].ToString();
                txtserviceTax4crtl.Width = Unit.Pixel(70);
                txtserviceTax4crtl.Visible = false;
                txtserviceTax4crtl.CssClass = "DisableInputNumber";
                txtserviceTax4crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");

                HtmlTableCell cell20 = new HtmlTableCell();
                cell20.ID = listtdserviceTax5crtl[i].ToString();
                cell20.Align = "Center";
                cell20.Visible = false;

                TextBox txtserviceTax5crtl = new TextBox();
                txtserviceTax5crtl.ID = listtxtserviceTax5crtl[i].ToString();
                txtserviceTax5crtl.Width = Unit.Pixel(70);
                txtserviceTax5crtl.Visible = false;
                txtserviceTax5crtl.CssClass = "DisableInputNumber";
                txtserviceTax5crtl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.ID,event);");

                HtmlTableCell cell21 = new HtmlTableCell();
                cell21.ID = listtdTaxincGrandTotalctrl[i].ToString();
                cell21.Align = "Center";
                cell21.Visible = false;

                TextBox txtTaxincGrandTotalctrl = new TextBox();
                txtTaxincGrandTotalctrl.ID = listtxtTaxincGrandTotalctrl[i].ToString();
                txtTaxincGrandTotalctrl.Width = Unit.Pixel(120);
                txtTaxincGrandTotalctrl.Visible = false;
                txtTaxincGrandTotalctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.ID,event);");
                txtTaxincGrandTotalctrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");
                txtTaxincGrandTotalctrl.CssClass = "inputnumber";

                cell1.Controls.Add(ddlservice);
                cell1.Controls.Add(hidTaxableNonTaxable);
                cell2.Controls.Add(txtdescriptionctrl);
                cell3.Controls.Add(txtyearrefctrl);
                cell4.Controls.Add(txtuomctrl);
                cell5.Controls.Add(txtunit2ctrl);
                cell6.Controls.Add(txtunit3ctrl);
                cell7.Controls.Add(txtunit4ctrl);
                cell8.Controls.Add(txtunitrate1ctrl);
                cell9.Controls.Add(txtunitrate2ctrl);
                cell10.Controls.Add(txtgrossamtctrl);
                cell10.Controls.Add(hidServiceCharge);
                cell11.Controls.Add(txtdiscount1ctrl);
                cell12.Controls.Add(txtdiscount2ctrl);
                cell13.Controls.Add(txtservicecharge1crtl);
                cell14.Controls.Add(txtservicecharge2crtl);
                cell15.Controls.Add(txtservicecharge3crtl);
                cell16.Controls.Add(txtserviceTax1crtl);
                cell17.Controls.Add(txtserviceTax2crtl);
                cell18.Controls.Add(txtserviceTax3crtl);
                cell19.Controls.Add(txtserviceTax4crtl);
                cell20.Controls.Add(txtserviceTax5crtl);
                cell21.Controls.Add(txtTaxincGrandTotalctrl);

                row.Cells.Add(cell1);
                row.Cells.Add(cell2);
                row.Cells.Add(cell3);
                row.Cells.Add(cell4);
                row.Cells.Add(cell5);
                row.Cells.Add(cell6);
                row.Cells.Add(cell7);
                row.Cells.Add(cell8);
                row.Cells.Add(cell9);
                row.Cells.Add(cell10);
                row.Cells.Add(cell11);
                row.Cells.Add(cell12);
                row.Cells.Add(cell13);
                row.Cells.Add(cell14);
                row.Cells.Add(cell15);
                row.Cells.Add(cell16);
                row.Cells.Add(cell17);
                row.Cells.Add(cell18);
                row.Cells.Add(cell19);
                row.Cells.Add(cell20);
                row.Cells.Add(cell21);

                tblmain.Rows.Add(row);
            }

        }
    }

    protected void BindTax()
    {
        txtservicetaxpercent.Text = string.Empty;
        hidservicetaxpercent.Value = string.Empty;
        txtpecesspercent.Text = string.Empty;
        hidpecesspercent.Value = string.Empty;
        txthecesspercent.Text = string.Empty;
        hidhecesspercent.Value = string.Empty;
        txtluxuarytaxpercent.Text = string.Empty;
        hidluxuarytaxpercent.Value = string.Empty;

        string caldate = "";
        if (txtdate.Text.ToString() != string.Empty)
        {
            string str = txtdate.Text.ToString();
            string[] s = str.Split('/');
            caldate = s[1] + "/" + s[0] + "/" + s[2].ToString();
        }
       
        SqlParameter[] parmtax = { 
                                   new SqlParameter("@CURRENTDATE",caldate)
                                 };
        DataTable dtSerTax = DU.getdatausingprocedure("USP_IVC_GetCurrentServiceTaxRate", parmtax);
        if (dtSerTax.Rows.Count > 0)
        {
            txtservicetaxpercent.Text = dtSerTax.Rows[0]["SERVICETAX"].ToString();
            hidservicetaxpercent.Value = dtSerTax.Rows[0]["SERVICETAX_LID"].ToString();
            txtpecesspercent.Text = dtSerTax.Rows[0]["PCESS"].ToString();
            hidpecesspercent.Value = dtSerTax.Rows[0]["PCESS_LID"].ToString();
            txthecesspercent.Text = dtSerTax.Rows[0]["HCESS"].ToString();
            hidhecesspercent.Value = dtSerTax.Rows[0]["HCESS_LID"].ToString();

            txtluxuarytaxpercent.Text = "0";
            hidluxuarytaxpercent.Value = "0";
          
        }
        //txtluxuarytaxpercent.Text = string.Empty;
        //hidluxuarytaxpercent.Value = string.Empty;
    }
    protected void BTNDATE_CLICK(object sender, EventArgs e)
    {
        BindTax();
    }

    protected void BindCustomerPrefence()
    {
        if (ddlCutstomer.SelectedValue != "0" && ddlFormat.SelectedValue != "0")
        {
            SqlParameter[] parma = {       //// Get CustomerPreferenceid and Credit Days for PreLoad Rows by Customer PreferenceCild Table
                                new SqlParameter("@CUSTOMERLEDGERID",ddlCutstomer.SelectedValue),
                                new SqlParameter("@FORMATID",ddlFormat.SelectedValue)
                               };
            DataTable dtCustomerprefid = DU.getdatausingprocedure("USP_IVC_GetCustomerPreference", parma);
            if (dtCustomerprefid.Rows.Count > 0)
            {
                if (Convert.ToInt32(dtCustomerprefid.Rows[0]["CUSTOMERPREID"]) > 0)
                {

                    hidCustomerprefid.Value = dtCustomerprefid.Rows[0]["CUSTOMERPREID"].ToString();
                    if (dtCustomerprefid.Rows[0]["CRDAYS"].ToString() != string.Empty)
                    {
                        txtcrdays.Text = dtCustomerprefid.Rows[0]["CRDAYS"].ToString();
                    }
                    else
                    {
                        txtcrdays.Text = "0";
                    }
                }
            }
            if (hidCustomerprefid.Value != string.Empty)
            {
                SqlParameter[] parmaa = { 
                                 new SqlParameter("@CUSTOMERPREID",hidCustomerprefid.Value)
                                    };
                DataTable dtPreLoadRows = DU.getdatausingprocedure("USP_IVC_GetCustomerPreferenceServices", parmaa);
                if (dtPreLoadRows.Rows.Count > 0)
                {
                    for (int i = 2; i < dtPreLoadRows.Rows.Count; i++)
                    {
                        ADDMoreRow();
                        GetDynamicColumn();
                    }
                    BindServiceRows(dtPreLoadRows);
                }
            }
        }
    }
    protected void BindServiceRows(DataTable dtPreLoadRows)
    {
        SqlParameter[] parma = { 
                                new SqlParameter("@Formatid",ddlFormat.SelectedValue)
                               };
        DataTable dtcol = DU.getdatausingprocedure("Usp_GetTransactionFormat_EnableDisable", parma);
        if (dtPreLoadRows.Rows.Count > 0)
        {
            for (int i = 1; i <= dtPreLoadRows.Rows.Count; i++)
            {
                DropDownList ddlservice = (DropDownList)tblmain.Rows[i].FindControl("ddlservice" + i.ToString());
                ddlservice.SelectedValue = Convert.ToString(dtPreLoadRows.Rows[i-1]["SERVICEID"]);
                ddlservice_SelectedIndexChanged(ddlservice, EventArgs.Empty);

                HiddenField hidTaxableNonTaxable = (HiddenField)tblmain.Rows[i].FindControl("hidTaxableNonTaxable" + i.ToString());
                if (dtcol.Rows[0]["Adddesc"].ToString() == "True")
                {
                    TextBox txtdescriptionctrl = (TextBox)tblmain.Rows[i].FindControl("txtdescriptionctrl" + i.ToString());
                }
                if (dtcol.Rows[0]["Yearreference"].ToString() == "True")
                {
                    TextBox txtyearrefctrl = (TextBox)tblmain.Rows[i].FindControl("txtyearrefctrl" + i.ToString());
                }
                if (dtcol.Rows[0]["uom"].ToString() == "True")
                {
                    TextBox txtuomctrl = (TextBox)tblmain.Rows[i].FindControl("txtuomctrl" + i.ToString());
                    txtuomctrl.Text = Convert.ToString(dtPreLoadRows.Rows[i-1]["UOM"]);
                }
                if (dtcol.Rows[0]["unit2"].ToString() == "True")
                {
                    TextBox txtunit2ctrl = (TextBox)tblmain.Rows[i].FindControl("txtunit2ctrl" + i.ToString());
                    txtunit2ctrl.Text = Convert.ToString(dtPreLoadRows.Rows[i-1]["UNIT2"]);
                }
                if (dtcol.Rows[0]["unit3"].ToString() == "True")
                {
                    TextBox txtunit3ctrl = (TextBox)tblmain.Rows[i].FindControl("txtunit3ctrl" + i.ToString());
                    txtunit3ctrl.Text = Convert.ToString(dtPreLoadRows.Rows[i-1]["UNIT3"]);
                }
                if (dtcol.Rows[0]["unit4"].ToString() == "True")
                {
                    TextBox txtunit4ctrl = (TextBox)tblmain.Rows[i].FindControl("txtunit4ctrl" + i.ToString());
                    txtunit4ctrl.Text = Convert.ToString(dtPreLoadRows.Rows[i-1]["UNIT4"]);
                }
                if (dtcol.Rows[0]["unitrate"].ToString() == "True")
                {
                    TextBox txtunitrate1ctrl = (TextBox)tblmain.Rows[i].FindControl("txtunitrate1ctrl" + i.ToString());

                    txtunitrate1ctrl.Text = Convert.ToString(dtPreLoadRows.Rows[i-1]["UNIT"]);

                    TextBox txtunitrate2ctrl = (TextBox)tblmain.Rows[i].FindControl("txtunitrate2ctrl" + i.ToString());

                    txtunitrate2ctrl.Text = Convert.ToString(dtPreLoadRows.Rows[i-1]["RATE"]);
                }
                TextBox txtgrossamtctrl = (TextBox)tblmain.Rows[i].FindControl("txtgrossamtctrl" + i.ToString());
                if (dtcol.Rows[0]["Discountlevel"].ToString() == "2")
                {
                    TextBox txtdiscount1ctrl = (TextBox)tblmain.Rows[i].FindControl("txtdiscount1ctrl" + i.ToString());
                    TextBox txtdiscount2ctrl = (TextBox)tblmain.Rows[i].FindControl("txtdiscount2ctrl" + i.ToString());
                }
                if (dtcol.Rows[0]["ShowserviceCharge"].ToString() == "True")
                {
                    TextBox txtservicecharge1crtl = (TextBox)tblmain.Rows[i].FindControl("txtservicecharge1crtl" + i.ToString());
                    TextBox txtservicecharge2crtl = (TextBox)tblmain.Rows[i].FindControl("txtservicecharge2crtl" + i.ToString());
                    TextBox txtservicecharge3crtl = (TextBox)tblmain.Rows[i].FindControl("txtservicecharge3crtl" + i.ToString());
                }
                if (dtcol.Rows[0]["Taxlevel"].ToString() == "True")
                {
                    TextBox txtserviceTax1crtl = (TextBox)tblmain.Rows[i].FindControl("txtserviceTax1crtl" + i.ToString());
                    TextBox txtserviceTax2crtl = (TextBox)tblmain.Rows[i].FindControl("txtserviceTax2crtl" + i.ToString());
                    TextBox txtserviceTax3crtl = (TextBox)tblmain.Rows[i].FindControl("txtserviceTax3crtl" + i.ToString());
                    if (dtcol.Rows[0]["ItemTaxType"].ToString() == "1")
                    {
                        TextBox txtserviceTax4crtl = (TextBox)tblmain.Rows[i].FindControl("txtserviceTax4crtl" + i.ToString());
                    }
                    else if (dtcol.Rows[0]["ItemTaxType"].ToString() == "2")
                    {
                        TextBox txtserviceTax5crtl = (TextBox)tblmain.Rows[i].FindControl("txtserviceTax5crtl" + i.ToString());
                    }
                }
                if (dtcol.Rows[0]["Taxinclusive"].ToString() == "True" || dtcol.Rows[0]["ItemTaxType"].ToString() == "0")
                {
                    TextBox txtTaxincGrandTotalctrl = (TextBox)tblmain.Rows[i].FindControl("txtTaxincGrandTotalctrl" + i.ToString());
                }
            }
        }
    }

    protected void ClearTable()
    {
        listcounter = (List<string>)ViewState["listcounter"];

        listtdservicectrl = (List<string>)ViewState["listtdservicectrl"];
        listddlservice = (List<string>)ViewState["listddlservice"];

        listtddescriptionctrl = (List<string>)ViewState["listtddescriptionctrl"];
        listtxtdescriptionctrl = (List<string>)ViewState["listtxtdescriptionctrl"];

        listtdyearrefctrl = (List<string>)ViewState["listtdyearrefctrl"];
        listtxtyearrefctrl = (List<string>)ViewState["listtxtyearrefctrl"];

        listtduomctrl = (List<string>)ViewState["listtduomctrl"];
        listtxtuomctrl = (List<string>)ViewState["listtxtuomctrl"];

        listtdunit2ctrl = (List<string>)ViewState["listtdunit2ctrl"];
        listtxtunit2ctrl = (List<string>)ViewState["listtxtunit2ctrl"];
        listtdunit3ctrl = (List<string>)ViewState["listtdunit3ctrl"];
        listtxtunit3ctrl = (List<string>)ViewState["listtxtunit3ctrl"];
        listtdunit4ctrl = (List<string>)ViewState["listtdunit4ctrl"];
        listtxtunit4ctrl = (List<string>)ViewState["listtxtunit4ctrl"];

        listtdunitrate1ctrl = (List<string>)ViewState["listtdunitrate1ctrl"];
        listtxtunitrate1ctrl = (List<string>)ViewState["listtxtunitrate1ctrl"];
        listtdunitrate2ctrl = (List<string>)ViewState["listtdunitrate2ctrl"];
        listtxtunitrate2ctrl = (List<string>)ViewState["listtxtunitrate2ctrl"];

        listtdgrossamtctrl = (List<string>)ViewState["listtdgrossamtctrl"];
        listtxtgrossamtctrl = (List<string>)ViewState["listtxtgrossamtctrl"];

        listtddiscount1ctrl = (List<string>)ViewState["listtddiscount1ctrl"];
        listtxtdiscount1ctrl = (List<string>)ViewState["listtxtdiscount1ctrl"];

        listtddiscount2ctrl = (List<string>)ViewState["listtddiscount2ctrl"];
        listtxtdiscount2ctrl = (List<string>)ViewState["listtxtdiscount2ctrl"];

        listtdservicecharge1crtl = (List<string>)ViewState["listtdservicecharge1crtl"];
        listtxtservicecharge1crtl = (List<string>)ViewState["listtxtservicecharge1crtl"];
        listtdservicecharge2crtl = (List<string>)ViewState["listtdservicecharge2crtl"];
        listtxtservicecharge2crtl = (List<string>)ViewState["listtxtservicecharge2crtl"];
        listtdservicecharge3crtl = (List<string>)ViewState["listtdservicecharge3crtl"];
        listtxtservicecharge3crtl = (List<string>)ViewState["listtxtservicecharge3crtl"];

        listtdserviceTax1crtl = (List<string>)ViewState["listtdserviceTax1crtl"];
        listtxtserviceTax1crtl = (List<string>)ViewState["listtxtserviceTax1crtl"];
        listtdserviceTax2crtl = (List<string>)ViewState["listtdserviceTax2crtl"];
        listtxtserviceTax2crtl = (List<string>)ViewState["listtxtserviceTax2crtl"];
        listtdserviceTax3crtl = (List<string>)ViewState["listtdserviceTax3crtl"];
        listtxtserviceTax3crtl = (List<string>)ViewState["listtxtserviceTax3crtl"];
        listtdserviceTax4crtl = (List<string>)ViewState["listtdserviceTax4crtl"];
        listtxtserviceTax4crtl = (List<string>)ViewState["listtxtserviceTax4crtl"];
        listtdserviceTax5crtl = (List<string>)ViewState["listtdserviceTax5crtl"];
        listtxtserviceTax5crtl = (List<string>)ViewState["listtxtserviceTax5crtl"];

        listtdTaxincGrandTotalctrl = (List<string>)ViewState["listtdTaxincGrandTotalctrl"];
        listtxtTaxincGrandTotalctrl = (List<string>)ViewState["listtxtTaxincGrandTotalctrl"];
        if (tblmain.Rows.Count > 3)
        {
            for (int i = tblmain.Rows.Count; i > 3; i--)
            {
                if (listcounter != null)
                {
                    listcounter.RemoveAt(i - 4);

                    listtdservicectrl.RemoveAt(i - 4);
                    listddlservice.RemoveAt(i - 4);

                    listtddescriptionctrl.RemoveAt(i - 4);
                    listtxtdescriptionctrl.RemoveAt(i - 4);
                    listtdyearrefctrl.RemoveAt(i - 4);
                    listtxtyearrefctrl.RemoveAt(i - 4);

                    listtduomctrl.RemoveAt(i - 4);
                    listtxtuomctrl.RemoveAt(i - 4);

                    listtdunit2ctrl.RemoveAt(i - 4);
                    listtxtunit2ctrl.RemoveAt(i - 4);
                    listtdunit3ctrl.RemoveAt(i - 4);
                    listtxtunit3ctrl.RemoveAt(i - 4);
                    listtdunit4ctrl.RemoveAt(i - 4);
                    listtxtunit4ctrl.RemoveAt(i - 4);

                    listtdunitrate1ctrl.RemoveAt(i - 4);
                    listtxtunitrate1ctrl.RemoveAt(i - 4);
                    listtdunitrate2ctrl.RemoveAt(i - 4);
                    listtxtunitrate2ctrl.RemoveAt(i - 4);

                    listtdgrossamtctrl.RemoveAt(i - 4);
                    listtxtgrossamtctrl.RemoveAt(i - 4);

                    listtddiscount1ctrl.RemoveAt(i - 4);
                    listtxtdiscount1ctrl.RemoveAt(i - 4);

                    listtddiscount2ctrl.RemoveAt(i - 4);
                    listtxtdiscount2ctrl.RemoveAt(i - 4);

                    listtdservicecharge1crtl.RemoveAt(i - 4);
                    listtxtservicecharge1crtl.RemoveAt(i - 4);
                    listtdservicecharge2crtl.RemoveAt(i - 4);
                    listtxtservicecharge2crtl.RemoveAt(i - 4);
                    listtdservicecharge3crtl.RemoveAt(i - 4);
                    listtxtservicecharge3crtl.RemoveAt(i - 4);

                    listtdserviceTax1crtl.RemoveAt(i - 4);
                    listtxtserviceTax1crtl.RemoveAt(i - 4);
                    listtdserviceTax2crtl.RemoveAt(i - 4);
                    listtxtserviceTax2crtl.RemoveAt(i - 4);
                    listtdserviceTax3crtl.RemoveAt(i - 4);
                    listtxtserviceTax3crtl.RemoveAt(i - 4);
                    listtdserviceTax4crtl.RemoveAt(i - 4);
                    listtxtserviceTax4crtl.RemoveAt(i - 4);
                    listtdserviceTax5crtl.RemoveAt(i - 4);
                    listtxtserviceTax5crtl.RemoveAt(i - 4);

                    listtdTaxincGrandTotalctrl.RemoveAt(i - 4);
                    listtxtTaxincGrandTotalctrl.RemoveAt(i - 4);
                }
                tblmain.Rows.RemoveAt(i - 1);
            }
            hcount.Value = "2";
            ResetTableControl(tblmain);
        }
    }
    protected void ResetTableControl(Control Tableid)
    {
        foreach (Control c in Tableid.Controls)
        {
            if (c.GetType() == typeof(TextBox))
            {
                ((TextBox)(c)).Text = string.Empty;
            }
            if (c.GetType() == typeof(HiddenField))
            {
                ((HiddenField)(c)).Value = string.Empty;
            }
            if (c.GetType() == typeof(DropDownList))
            {
                ((DropDownList)(c)).SelectedIndex = 0;
            }
            if (c.HasControls())
            {
                ResetTableControl(c);
            }
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Server.Transfer("~/Welcome.aspx");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

    }
}

//var TaxGrossAmt=parseFloat(parseFloat(parseFloat(parseFloat(servicetaxper)+(parseFloat(pecesstaxper)/10)+(parseFloat(hecesstaxper)/10)*100)/1000)/10);
//alert((parseFloat(parseFloat(servicetaxper)+(parseFloat(pecesstaxper)/10)+(parseFloat(hecesstaxper)/10))*100/1000/10));

 //var TaxGrossAmt=(parseFloat(parseFloat(servicetaxper)+(parseFloat(pecesstaxper)/10)+(parseFloat(hecesstaxper)/10))*100/1000/10)+1;